package com.voc.cpnc;

import com.voc.cpnc.utils.JsonUtils;
import com.voc.cpnc.utils.LogUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class DXSYPlatform {


    private static char[] MAP1 = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 9, '[', ']', '\\', '{', '}', '|', ';', '\'', ':', '\"', ',', '.', '/', '<', '>', '?', ' '};
    private static char[] MAPTO1 = {192, 191, 139, 140, 7, 138, 137, 141, 142, 8, 143, 134, 181, 135, 174, 180, 6, 179, 175, 136, 176, 182, 183, 5, 184, 177, 30, 178, 185, 31, 127, 187, 29, 189, 188, 173, 190, 186, 133, 172, 144, 171, 16, 132, 18, 17, 145, 170, 169, 146, 168, 147, 148, 167, 166, 149, 150, 165, 4, 164, 163, 151, 152, 153, 3, 14, 26, 19, 154, 162, 27, 128, 28, 129, 11, 130, 25, 131, 1, 161, 160, 12, 2, 193, 159, 20, 158, 21, 22, 157, 15, 155, 23, 156, 24, 0};

    /**
     * 设置手机号绑定设备
     * 参数：{"p":"18396125005,18396125656\tA21UUQGhXORwxBJ2","trsCode":"updatePhoneNoZcNos"}
     * 返回：{}
     */
    public static void updatePhoneNoZcNos() {

        String trsCode = "updatePhoneNoZcNos";
        String p = "18396125005" + "\t" + "A21UUQGhXORwxBJ2";//手机号\t设备ID（多个手机号用英文逗号隔开）

        commonConnection(trsCode, p);
    }

    /**
     * 刷新（）
     * 参数：{"p":"A21UUQGhXORwxBJ2","trsCode":"imAlive"}
     * 返回：{"r":"0.66\n6\n0.66\n2020-06-19 17:14:29\n8\n0.66\nY\n0.00\n3668\n0\nhttp://xunyaosoft.com/zcnosdl.html\n"}
     */
    public static void imAlive() {
        //sokuuec6zxda5v6r
        String trsCode = "imAlive";
        String p = "sokuuec6zxda5y6r";//设备ID

        commonConnection(trsCode, p);
    }


    /**
     * 发送短信信息
     * 参数：{"p":"440\n1592559979072\n【今日头条极速版】验证码9586，用于手机登录，5分钟内有效。验证码提供给他人可能导致帐号被盗，请勿泄露，谨防被骗。\n1069408503813391\n18396125005\nA21UUQGhXORwxBJ2","trsCode":"saveSms"}
     * 返回：{"r":"Y"}
     */
    public static void saveSms() {

        String trsCode = "saveSms";
        String p = "441" + "\n" +//短信id（自增长即可）
                //时间戳
                System.currentTimeMillis() + "\n" +
                //短信内容
                "【京东】验证码5874，用于手机登录，5分钟内有效。验证码提供给他人可能导致帐号被盗，请勿泄露，谨防被骗。" + "\n" +
                //短信号码
                "1069408503813391" + "\n" +
                //绑定的手机号
                "16711688550" + "\n" +
                //设备ID
                "92kziezegcrxly0f";

        commonConnection(trsCode, p);
    }

    /**
     * 绑定微信//
     * 参数：{"p":"061zhiaJ0UadDd2z2PbJ0tO5aJ0zhiaY\t9RIdLJI0044w355f","trsCode":"bindWxAcctZcNos"}
     */
    public static void bindWxAcctZcNos() {

        String trsCode = "bindWxAcctZcNos";
        String p = "021GYFq92GgElN0aHps924SKq92GYFqA\t9RIdLJI0044w355f";//"wxOpenid" + "\t" + "设备ID"

        commonConnection(trsCode, p);
    }

    /**
     * 提现
     * 参数：{"p":"A21UUQGhXORwxBJ2","trsCode":"withdrawZcNos"}
     * 返回：{"exception":{"detailMessage":"最低提现0.3元","stackTrace":[],"suppressedExceptions":[]}}
     */
    public static void withdrawZcNos() {

        String trsCode = "withdrawZcNos";
        String p = "1hew3zahmm6lavdx";//设备ID

        commonConnection(trsCode, p);
    }


    public static String commonConnection(String trsCode, String p) {
        String param = JsonUtils.objectToString(new CommonParam(trsCode, p));
        try {
            HttpURLConnection postConnection = (HttpURLConnection) new URL("http://120.78.198.87/zc/zhicode/basereq/get.php").openConnection();
            postConnection.setRequestMethod("POST");
            postConnection.setConnectTimeout(5000);
            postConnection.setReadTimeout(5000);
            postConnection.setDoInput(true);
            postConnection.setDoOutput(true);
            postConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            OutputStream outputStream = postConnection.getOutputStream();
            outputStream.write(("time_stamp=" + System.currentTimeMillis() + "&type=get&data_content=&verification=randp&zcpost=" + encode(urlencode(param))).getBytes());
            outputStream.flush();
            outputStream.close();
            StringBuilder buffer = new StringBuilder();
            if (postConnection.getResponseCode() == 200) {
                InputStream inputStream = postConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                while (true) {
                    String line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    buffer.append(line);
                }
                String result = urldecode(decode(buffer.toString()));
                LogUtils.e("返回结果：".concat(result));
                bufferedReader.close();
                inputStream.close();
                return result;
            } else {
                LogUtils.e("网络错误");
            }
            postConnection.disconnect();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.e("=======================" + e.getMessage());

            return null;
        }
    }


//    public static void uploadSMS() {
//
//        try {
//            //A21UUQGhXORwxBJ2
//            //3b86ca7aaf7b6793
//            String code = JsonUtils.objectToString(new CommonParam("saveSms", 440 + "\n" + System.currentTimeMillis() + "\n" + "【今日头条极速版】验证码8596，用于手机登录，5分钟内有效。验证码提供给他人可能导致帐号被盗，请勿泄露，谨防被骗。" + "\n" + "1069408503813391" + "\n" + "18396125005" + "\n" + "A21UUQGhXORwxBJ2"));
////            String gparam = encode(urlencode("{\"p\":\"435\\n1592552771649\\n【今日头条极速版】验证码8888，用于手机登录，5分钟内有效。验证码提供给他人可能导致帐号被盗，请勿泄露，谨防被骗。\\n1069408503813391\\n13599384500\\n3b86ca7aaf7b6793\",\"trsCode\":\"saveSms\"}"));
//            String gparam = encode(urlencode(code));
//            HttpURLConnection postConnection = (HttpURLConnection) new URL("http://120.78.198.87/zc/zhicode/basereq/get.php").openConnection();
//            postConnection.setRequestMethod("POST");
//            postConnection.setConnectTimeout(5000);
//            postConnection.setReadTimeout(5000);
//            postConnection.setDoInput(true);
//            postConnection.setDoOutput(true);
//            postConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
//            OutputStream outputStream = postConnection.getOutputStream();
//            outputStream.write(("time_stamp=" + System.currentTimeMillis() + "&type=get&data_content=&verification=randp&zcpost=" + gparam).getBytes());
//            outputStream.flush();
//            outputStream.close();
//            StringBuilder buffer = new StringBuilder();
//            if (postConnection.getResponseCode() == 200) {
//                InputStream inputStream = postConnection.getInputStream();
//                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                while (true) {
//                    String line = bufferedReader.readLine();
//                    if (line == null) {
//                        break;
//                    }
//                    buffer.append(line);
//                }
//                LogUtils.e("返回信息：".concat(urldecode(decode(buffer.toString()))));
//                bufferedReader.close();
//                inputStream.close();
//            } else {
//                LogUtils.e("网络错误");
//            }
//            postConnection.disconnect();
//        } catch (IOException e) {
//            e.printStackTrace();
//            LogUtils.e("=================" + e.getMessage());
//        }
//
//    }

    public static String urldecode(String result) {
        try {
            return URLDecoder.decode(result, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return result;
        }
    }

    public static String urlencode(String param) {
        try {
            return URLEncoder.encode(param.replaceAll("\\+", "%2B"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return param;
        }
    }

    public static String encode(String s) {
        for (int i = 0; i < MAP1.length; i++) {
            while (s.indexOf(MAP1[i]) != -1) {
                s = s.replace(MAP1[i], MAPTO1[i]);
            }
        }
        return s;
    }

    public static String decode(String s) {
        for (int i = 0; i < MAP1.length; i++) {
            while (s.indexOf(MAPTO1[i]) != -1) {
                s = s.replace(MAPTO1[i], MAP1[i]);
            }
        }
        return s;
    }


    public static class CommonParam {
        private String trsCode;
        private String p;

        public CommonParam(String trsCode, String p) {
            this.trsCode = trsCode;
            this.p = p;
        }

        public String getTrsCode() {
            return trsCode;
        }

        public void setTrsCode(String trsCode) {
            this.trsCode = trsCode;
        }

        public String getP() {
            return p;
        }

        public void setP(String p) {
            this.p = p;
        }
    }

}
